package com.onlineExam.question.service.impl;

import java.util.List;
import com.onlineExam.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.onlineExam.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.onlineExam.question.domain.Option;
import com.onlineExam.question.mapper.QuestionMapper;
import com.onlineExam.question.domain.Question;
import com.onlineExam.question.service.IQuestionService;

/**
 * 题库管理Service业务层处理
 * 
 * @author amy
 * @date 2025-05-30
 */
@Service
public class QuestionServiceImpl implements IQuestionService 
{
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询题库管理
     * 
     * @param id 题库管理主键
     * @return 题库管理
     */
    @Override
    public Question selectQuestionById(Long id)
    {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询题库管理列表
     * 
     * @param question 题库管理
     * @return 题库管理
     */
    @Override
    public List<Question> selectQuestionList(Question question)
    {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增题库管理
     * 
     * @param question 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuestion(Question question)
    {
        question.setCreateTime(DateUtils.getNowDate());
        int rows = questionMapper.insertQuestion(question);
        insertOption(question);
        return rows;
    }

    /**
     * 修改题库管理
     * 
     * @param question 题库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuestion(Question question)
    {
        question.setUpdateTime(DateUtils.getNowDate());
        questionMapper.deleteOptionByQuestionId(question.getId());
        insertOption(question);
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除题库管理
     * 
     * @param ids 需要删除的题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionByIds(Long[] ids)
    {
        questionMapper.deleteOptionByQuestionIds(ids);
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除题库管理信息
     * 
     * @param id 题库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionById(Long id)
    {
        questionMapper.deleteOptionByQuestionId(id);
        return questionMapper.deleteQuestionById(id);
    }

    /**
     * 新增题目选项信息
     * 
     * @param question 题库管理对象
     */
    public void insertOption(Question question)
    {
        List<Option> optionList = question.getOptionList();
        Long id = question.getId();
        if (StringUtils.isNotNull(optionList))
        {
            List<Option> list = new ArrayList<Option>();
            for (Option option : optionList)
            {
                option.setQuestionId(id);
                list.add(option);
            }
            if (list.size() > 0)
            {
                questionMapper.batchOption(list);
            }
        }
    }
}
